package com.shawn.rabbitmq.publishsubsribemode;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName ReceiveLogs01
 * @Description TODO
 * @Author shawn
 * @date 2021/9/22 9:53 下午
 * @Version 1.0
 */
public class ReceiveLogs01 {
    private final static String exchange_name = "logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.64.12");
        factory.setUsername("shawn");
        factory.setPassword("shawn");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        /**
         * 生成一个临时队列 队列名称是随机的
         * 当消费者断开和该队列的连接时，队列自动删除
         */
        String queueName = channel.queueDeclare().getQueue();
        //交换机和队列绑定
        channel.queueBind(queueName, EmitLog.EXCHANGE_NAME, "");
        DeliverCallback deliverCallback = (consumerTag, message)->{
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println("消费者一接收到消息："+ msg);
        };
        CancelCallback cancelCallback = consumerTag->{};
        channel.basicConsume(queueName, true, deliverCallback, cancelCallback);
    }
}
